package com.tanhua.manage.listener;

import com.alibaba.fastjson.JSON;
import com.tanhua.commons.templates.HuaWeiUGCTemplate;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.dubbo.api.mongo.PublishApi;
import com.tanhua.manage.domain.Log;
import com.tanhua.manage.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
@Slf4j
@RocketMQMessageListener(topic = "tanhua-publish",consumerGroup = "tanhua-publish-consumer")
public class PublishListener implements RocketMQListener<String> {

    @Reference
    private PublishApi publishApi;

    @Autowired
    private HuaWeiUGCTemplate template;

    @Override
    public void onMessage(String publishId) {
        log.info("=======动态审核========");
        //1. 根据id查找动态
        Publish publish = publishApi.findById(publishId);

        //2. 审核动态文本与图片
        int state = publish.getState();
        if(template.textContentCheck(publish.getTextContent())){
            if(template.imageContentCheck(publish.getMedias().toArray(new String[]{}))){
                state=1;
            }else{
                state=0; // 图片审核不通过
            }

        }else{
            state= 0; //文本内容被驳回
        }

        //3. 更新状态
        publishApi.updateState(publish,state);

    }
}
